<!--
@description ：路径管理
-->

<template>
  <div class="app-container">
    <div class="header-search">
      <NormalForm :formList="formList" :isShowCard="false" :operateForm="queryForm" pageTitle="路径管理"
                  @handleQuery="handleQuery" @handleReset="handleReset"></NormalForm>
    </div>
    <div class="set-content">
      <div style="text-align: right; margin: 12px 0;">
        <el-button icon="el-icon-plus" type="primary" @click="createPath">创建路径</el-button>
      </div>
      <el-table v-loading="loading" :data="tableList" max-height="560" min-height="420" style="width: 100%">
        <el-table-column align="center" prop="name" label="路径标题" min-width="200"></el-table-column>
        <el-table-column align="center" prop="path" label="小程序路径" min-width="200"></el-table-column>
        <el-table-column align="center" prop="pictureUrl" label="路径图片" min-width="180">
           <template slot-scope="scope">
             <img :src="scope.row.pictureUrl" alt="" style="width: 60px;height: 70px">
           </template>
        </el-table-column>
        <el-table-column align="center" prop="remark" label="备注" min-width="160"></el-table-column>
        <el-table-column align="center" prop="createBy" label="创建人" min-width="140"></el-table-column>
        <el-table-column align="center" prop="updateTime" label="更新时间" min-width="160"></el-table-column>
        <el-table-column label="操作" align="center" min-width="120">
          <template slot-scope="{ row }">
            <el-button type="text" @click="editPath(row)">编辑</el-button>
            <el-button type="text" @click="delPath(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <NormalPagination :currentPage="queryForm.pageNum" :pageSize="queryForm.pageSize" :total="total"
                        @handleCurrentChange="getListFn"
                        @handleSizeChange="getListFn"></NormalPagination>
    </div>
    <el-dialog :visible.sync="visible" :title="editFlag == '1'? '创建路径':'编辑路径'" append-to-body width="600px" :close-on-click-modal="false">
      <el-form ref="form" :rules="rules" :model="form" label-width="100px">
        <el-form-item label="路径标题" prop="name">
          <el-input v-model="form.name" clearable placeholder="请输入路径标题" style="width: 400px"></el-input>
        </el-form-item>
        <el-form-item label="小程序路径" prop="path">
          <el-input v-model="form.path" clearable placeholder="请输入小程序路径" style="width: 400px"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="3" maxlength="50" show-word-limit clearable placeholder="请输入备注" style="width: 400px"></el-input>
        </el-form-item>
        <el-form-item label="路径图片" required>
          <div class="upload">
            <el-upload
              action
              :before-upload="beforeUploadImg"
              :show-file-list="false"
              list-type="picture-card"
            >
              <i class="el-icon-plus"></i>
<!--              <div class="el-upload__tip" slot="tip" style="color: #CCCCCC">-->
<!--                上传图片大小不超过10MB-->
<!--              </div>-->
            </el-upload>
            <div v-for="(item, index) in imgs" class="img" :key="index" style="margin-left: 6px">
              <img v-if="item" :src="item" width="100%"/>
              <div class="imgPre">
                <i class="el-icon-zoom-in" @click="dialogVisible = true, dialogImageUrl = item"></i>
                <i class="el-icon-delete" @click="handleRemove(index)"></i>
              </div>
            </div>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false" :loading="btnLoading">取消</el-button>
        <el-button type="primary" @click="handelConfirm" :loading="btnLoading">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="删除确认" :visible.sync="delVisible" width="400px" :show-close="false" :close-on-click-modal="false">
      <div>确认删除该路径？</div>
      <div slot="footer">
        <el-button @click="delVisible = false">取 消</el-button>
        <el-button type="primary" @click="sureDel">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogVisible" title="图片预览" width="600px" append-to-body :close-on-click-modal="false">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>
<script>
import {uploadImgFile} from "@/api/communityOperating/fileManage";
import {
  delProgramPath,
  queryProgramPathDetail,
  queryProgramPathList,
  saveOrUpdateProgramPath
} from "@/api/smallProgram";
import moment from "moment";
export default {
  name: "smallProgramPath", //页面名称，必填
  props:{

  },
  components:{

  },
  data(){
    return{
      formList: [
        {
          label: '路径名称/备注',
          value: '',
          prop: 'name',
          type: 'INPUT',
          emunName: ''
        },
        {
          label: '',
          value: [],
          prop: 'selectTime',
          type: 'DATE_RANGE',
          emunName: '',
        },
      ],
      queryForm: {
        name: '',
        selectTime: [],
        pageNum: 1,
        pageSize: 20,
      },
      loading: false,
      tableList: [
        {name: '话梅小程序', url: require('@/assets/image/allot.png')},
        {name: '话梅小程序', url: require('@/assets/image/allot.png')},
        {name: '话梅小程序', url: require('@/assets/image/allot.png')},
      ],
      total: 0,
      visible: false, // 弹窗显隐
      editFlag: '1',
      form: {

      },
      rules: {
        name: [{required: true, message: '请输入路径标题', trigger: 'blur'},],
        path: [{required: true, message: '请输入小程序路径', trigger: 'blur'}]
      },
      imgs: [],
      delVisible: false,
      editId: '',
      dialogVisible: false,
      dialogImageUrl: '',
      currentPathId: '',
      btnLoading: false,
    }
  },
  watch:{

  },
  methods:{
    handleQuery(){
      this.queryForm.pageNum = 1
      this.getListFn()
    },
    handleReset(){
      this.queryForm = {
        name: '',
        selectTime: [],
        startTime: '',
        endTime: '',
        pageNum: 1,
        pageSize: 20,
      }
      this.getListFn()
    },
    getListFn(){
      let params = {
        ...this.queryForm,
        startTime: this.queryForm.selectTime && this.queryForm.selectTime.length == 2 ? moment(this.queryForm.selectTime[0]).format('YYYY-MM-DD') : '',
        endTime: this.queryForm.selectTime && this.queryForm.selectTime.length == 2 ? moment(this.queryForm.selectTime[1]).format('YYYY-MM-DD') : '',
        smallId: this.$route.query.id
      }
      this.loading = true
      queryProgramPathList(params).then(res => {
        if(res.code == 200){
          this.tableList = res.rows
          this.total = res.total
        }
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 创建路径
    createPath(){
      this.editFlag == '1'
      this.visible = true
      this.imgs = []
      this.editId = ''
      this.form = {
        name: '',
        path: '',
        remark: '',
      }
    },
    // 编辑
    editPath(row){
      this.editFlag = '2'
      this.visible = true
      this.editId = row.pathId
      queryProgramPathDetail({pathId: row.pathId}).then(res => {
        if(res.code == 200){
          this.form = { ...res.data}
          this.form.photoUrl = res.data.pictureUrl || ''
          this.imgs = [res.data.pictureUrl || '']
        }
      })
    },
    // 图片上传
    async beforeUploadImg(img) {
      console.log(img)
      const type = ['image/png', 'image/jpg', 'image/gif', 'image/jpeg']
      const isJPG = type.includes(img.type)
      const isLt10M = img.size / 1024 / 1024 < 10

      if (!isJPG) {
        this.$message.error('上传图片支持jpg、png、gif格式!')
      } else if (!isLt10M) {
        this.$message.error('您上传的文件超过10MB!')
      } else {
        console.log(img, '====img')
        const formData = new FormData()
        formData.append('file', img)
        const { data } = await uploadImgFile(formData)
        this.imgs = [window.globalConfig.IMAGE_API + data.url]
        console.log(this.imgs[0])
        this.form.photoName = data.photoName
        this.form.photoUrl = data.url
      }
    },
    // 图片删除
    handleRemove(index) {
      this.$confirm('是否确认删除所选图片？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.imgs.splice(index, 1)
      }).catch()
    },
    // 弹窗确定
    handelConfirm(){
      if(!this.imgs || this.imgs.length == 0) return this.$message.error('请上传路径图片')
      let params = {
        ...this.form,
        pathId: this.editId || '',
        smallId: this.$route.query.id,
        pictureUrl: this.imgs[0]
      }
      this.btnLoading = true
      this.$refs.form.validate(valid => {
        if(valid){
          saveOrUpdateProgramPath(params).then(res => {
            if(res.code == 200){
              this.$message.success(this.editId ? '编辑成功': '新增成功')
              this.visible = false
              this.handleQuery()
              this.btnLoading = false
            }
          }).catch(() => {
            this.btnLoading = false
          })
        }else{
          this.btnLoading = false
        }
      })
    },
    // 删除
    delPath(row){
       this.delVisible = true
       this.currentPathId = row.pathId
    },
    sureDel() {
      delProgramPath({ pathId: this.currentPathId}).then(res => {
        if(res.code == 200){
          this.delVisible = false
          this.$message.success('删除成功')
          this.handleQuery()
        }
      })
    }
  },
  mounted() {
    this.handleQuery()
  },
  created() {
  },
}

</script>

<style lang="scss" scoped>

.app-container{
  height: 100%;
  padding: 10px 0 0;
}

.header-search{
  padding: 10px;
  background-color: #fff;
  border-radius: 8px;
}

.set-content{
  padding: 10px 16px;
  margin-top: 18px;
  background-color: #fff;
  border-radius: 8px;
}

.img {
  position: relative;
  width: 148px;
  height: 148px;
  margin-right: 10px;

  img {
    height: 100%;
  }

  .imgPre{
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    background: rgba(238,238,238,0.4);
    display: flex;
    justify-content: space-around;
    align-items: center;
    opacity: 0;
    &:hover{
      opacity: 1;
    }
    i{
      font-size: 22px;
      color: #fff;
    }
  }
}

.upload {
  display: flex;
}

.switch-input{
  padding: 4px 0 0 10px!important;
  ::v-deep.el-form-item__content{
    line-height: 30px;
    display: flex;
    align-items: center;
  }
}
</style>
